package com.xiguaji.tiktok.v1.ins.favorite.service;

import com.xiguaji.boot.toolkit.jdbc.Page;
import com.xiguaji.tiktok.v1.ins.brand.repository.CommonBrandRepository;
import com.xiguaji.tiktok.v1.ins.config.InsConfig;
import com.xiguaji.tiktok.v1.ins.favorite.dto.BloggerDto;
import com.xiguaji.tiktok.v1.ins.favorite.dto.BrandDto;
import com.xiguaji.tiktok.v1.ins.favorite.dto.MediaDto;
import com.xiguaji.tiktok.v1.ins.favorite.dto.ProductDto;
import com.xiguaji.tiktok.v1.ins.favorite.model.InsFavorite;
import com.xiguaji.tiktok.v1.ins.favorite.repository.InsFavoriteRepository;
import com.xiguaji.tiktok.v1.ins.favorite.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class InsFavoriteService {
    @Autowired
    private InsFavoriteRepository insFavoriteRepository;
    @Autowired
    InsConfig insConfig;
    @Autowired
    CommonBrandRepository commonBrandRepository;

    public InsFavorite getByTypeAndTargetIdAndUserId(int type, long uid, int userId){
        return insFavoriteRepository.findByTypeAndTargetIdAndUserId(type,uid,userId);
    }

    public void save(InsFavorite insFavorite){
        insFavoriteRepository.save(insFavorite);
    }

    public void delFavorite(int id){
        insFavoriteRepository.deleteById(id);
    }

    public Page<BloggerDto> findBloggerPage(PageVo pageVo,long userId){
        return insFavoriteRepository.findBloggerPage(pageVo,userId);
    }

    public Page<ProductDto> findProductPage(PageVo pageVo,long userId){
        long startTime = System.currentTimeMillis() / 1000 - insConfig.getLatestDays() * 24 * 3600;
        return insFavoriteRepository.findProductPage(pageVo,userId,startTime,System.currentTimeMillis() / 1000);
    }

    public Page<MediaDto> findMediaPage(PageVo pageVo, long userId){
        return insFavoriteRepository.findMediaPage(pageVo,userId);
    }

    public Page<BrandDto> findBrandPage(PageVo pageVo, long userId){
        Page<BrandDto> page =insFavoriteRepository.findBrandPage(pageVo,userId);

        page.getList().forEach( brandResp -> {
            String logo = commonBrandRepository.findCommonBrandMoreFansBloggerPic(brandResp.getId());
            brandResp.setLogo(logo);
        });
        return page;
    }

    public int countFavorite(int type,int userId){
        return insFavoriteRepository.countByTypeAndUserId(type,userId);
    }

}
